package com.dream.banana.notify.support;
import java.util.Objects;
import org.aopalliance.aop.Advice;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.retry.support.RetryTemplate;

public final class NotifyConfigSupport implements INotifyConfigSupport {
    private static final Logger logger = LoggerFactory.getLogger(NotifyConfigSupport.class);
    private static final String[] CONFIG_NOTIFY = new String[] { "classpath*:applicationContext-notify-pub.xml" };
    private static final ApplicationContext mqContext;
    private ConnectionFactory connFactory = null;
    private boolean useDefaultConfig = true;

    public NotifyConfigSupport() {
        this.connFactory = (ConnectionFactory) mqContext.getBean("defaultConnectionFactory", ConnectionFactory.class);
    }

    public NotifyConfigSupport(NotifyConfig config) {
        try {
            Objects.requireNonNull(config.getAddresses(), "消息服务器地址不能为空！");
            logger.info("自定义配置参数：消息服务器地址--->{},消息服务器端口--->{},消息服务虚拟空间--->{},连接消息服务账号--->{},连接消息服务密码--->{}",
                    new Object[] { Integer.valueOf(config.getPort()), config.getAddresses(), config.getVirtualHost(),
                            config.getUsername(), config.getPassword() });
            CachingConnectionFactory e = new CachingConnectionFactory(config.getPort());
            e.setAddresses(config.getAddresses());
            e.setVirtualHost(config.getVirtualHost());
            e.setUsername(config.getUsername());
            e.setPassword(config.getPassword());
            this.connFactory = e;
            this.useDefaultConfig = false;
        } catch (NullPointerException arg2) {
            logger.error("初始化接收消息服务失败:", arg2);
            logger.info("使用默认配置!");
            this.connFactory = (ConnectionFactory) mqContext.getBean("defaultConnectionFactory",
                    ConnectionFactory.class);
        }

    }

    public ConnectionFactory getConnectionFactory() {
        return this.connFactory;
    }

    public RabbitAdmin getRabbitAdmin() {
        if (this.useDefaultConfig) {
            return (RabbitAdmin) mqContext.getBean("defaultContainerAdmin", RabbitAdmin.class);
        } else {
            RabbitAdmin rabbitAdmin = new RabbitAdmin(this.connFactory);
            RabbitTemplate raTemp = rabbitAdmin.getRabbitTemplate();
            raTemp.setRetryTemplate(this.getDefaultRetryTemplate());
            raTemp.setMessageConverter(this.getDefaultMessageConverter());
            return rabbitAdmin;
        }
    }

    public AmqpTemplate getAmqpTemplate() {
        if (this.useDefaultConfig) {
            return (AmqpTemplate) mqContext.getBean("defaultTemplate", AmqpTemplate.class);
        } else {
            RabbitTemplate raTemp = new RabbitTemplate(this.connFactory);
            raTemp.setRetryTemplate(this.getDefaultRetryTemplate());
            raTemp.setMessageConverter(this.getDefaultMessageConverter());
            return raTemp;
        }
    }

    public MessageConverter getDefaultMessageConverter() {
        return (MessageConverter) mqContext.getBean("defaultMessageConverter", MessageConverter.class);
    }

    public RetryTemplate getDefaultRetryTemplate() {
        return (RetryTemplate) mqContext.getBean("defaultRetryTemplate", RetryTemplate.class);
    }

    public Advice getDefaultAdvice() {
        return (Advice) mqContext.getBean("defaultRetryInterceptor", Advice.class);
    }

    static {
        mqContext = new ClassPathXmlApplicationContext(CONFIG_NOTIFY);
    }
}